package com.hil;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.hil.chain.ChainManager;
import com.hil.chain.Task1;
import com.hil.chain.Task2;
import com.hil.chain.Task3;
import com.hil.okhtttp.Call2;
import com.hil.okhtttp.Callback2;
import com.hil.okhtttp.OkHttpClient2;
import com.hil.okhtttp.R;
import com.hil.okhtttp.Request2;
import com.hil.okhtttp.Response2;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    ImageView iv_advise;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_advise = findViewById(R.id.iv_advise);

        findViewById(R.id.btn_image_browser).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startSAF();
                    }
                }
        );

        findViewById(R.id.btn_chain).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        testChain();
                    }
                }
        );
        findViewById(R.id.btn_okhttp).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        testOkhttp();
                    }
                }
        );
    }

    private void testChain(){
        ChainManager chainManager = new ChainManager();

        chainManager.addTask(new Task1());
        chainManager.addTask(new Task2());
        chainManager.addTask(new Task3());

        chainManager.doRunAction("end", chainManager);
    }


    private void startSAF() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        //选择图片
        intent.setType("image/jpeg");
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            //选中返回的图片封装在uri里
            Uri uri = data.getData();
            openUri(uri);
        }
    }

    private void openUri(Uri uri) {
        try {
            //从uri构造输入流
            InputStream fis = getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(fis);
            iv_advise.setImageBitmap(bitmap);
        } catch (Exception e) {

        }
    }

    void testOkhttp(){
        OkHttpClient2 okHttpClient2 = new OkHttpClient2.Builder().build();
        Request2 request2 = new Request2.Builder().url("http://www.baidu.com").get().build();
        Call2 call2 = okHttpClient2.newCall(request2);
        call2.enqueue(new Callback2() {
            @Override
            public void onFailure(Call2 call, IOException e) {
                Log.e("onFailure","自定义OKHTTP请求失败...." );
            }

            @Override
            public void onResponse(Call2 call, Response2 response) throws IOException {
                Log.e("onSuccess","OKHTTP请求成功.... result:" + response.string() );

            }
        });
    }
    /**
     // 创建OkHttpClient,构建者模式
     OkHttpClient httpClient = new OkHttpClient.Builder().build();

     // 创建Request,构建者模式
     Request request = new Request.Builder()
     .get()
     .url("https://www.baidu.com").build();

     // 创建一个Call
     Call realCall = httpClient.newCall(request);
     // 同步方法
     // Response response = realCall.execute();
     // String string = response.body().string();
     // Log.d(TAG, string);

     // 异步请求接口
     realCall.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
    // 请求失败
    e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
    // 请求成功
    // 打印响应的内容
    String string = response.body().string();
    Log.d(TAG, string);
    }
    });
     *
     * */
     // 不可再次执行realCall.enqueue方法,内部会抛出异常IllegalStateException
     /**
     * synchronized (this) {
     *       if (executed) throw new IllegalStateException("Already Executed");
     *       executed = true;
     * }
     */

}